<?php
namespace app\controllers;

use app\common\corelib\LocalUpload;
use app\logic\CategoryLogic;
use app\logic\GoodsLogic;
use yii;

class GoodsController extends BackendController
{
    const LIMIT=30;

   public function actionIndex(){
       $query = Yii::$app->request->get('form', []);
       $page = Yii::$app->request->get('page', 1);
       $goodsLogic = new GoodsLogic();
       $this->data['breadcrumbs'] = '商品库';
       return $this->render('index', [
           'goods' => $goodsLogic->getAllGoods($query, $page),
           'query' => $query
       ]);
   }

    public function actionAdd()
    {
        $goodsLogic = new GoodsLogic();
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['商品管理' => U('goods/index'), ' 添加商品' => ''];
            $categoryLogic = new CategoryLogic();
            $category = $categoryLogic->getCategorys();
            return $this->render('add', [
                'category'=>$category,
                'theme' => '/assets/',
                'actionUrl' => U('goods/add')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            if($_FILES['goods_listpic']['name']){
                LocalUpload::go_upload($_FILES['goods_listpic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $logicRes = $goodsLogic->addGoods($params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增商品成功');
                jump('goods/index');
            } else {
                Yii::$app->getSession()->setFlash('error','新增商品失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionEdit()
    {
        $goodsLogic = new GoodsLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('goods_id');
            $goods = $goodsLogic->getById($id);
            $this->data['breadcrumbs'] = ['商品管理' => U('goods/index'), $goods['goods_name'] => '', '更新' => ''];
            if(file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$goods['goods_listpic'])){
                $goods_listpic = $goods['goods_listpic'];
            }else{
                $goods_listpic = '/default.jpg';
            }
            $goods_descpic = explode(',',$goods['goods_descpic']);
            foreach($goods_descpic AS &$value){
                if(!file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$value)){
                    $value = '/default.jpg';
                }
            }
            $categoryLogic = new CategoryLogic();
            $category = $categoryLogic->getCategorys();
            return $this->render('edit', [
                'category'=>$category,
                'goods' => $goods,
                'goods_listpic' => $goods_listpic,
                'goods_descpic'=>$goods_descpic,
                'theme' => '/assets/',
                'actionUrl' => U('goods/edit'),
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('goods_id');

            if($_FILES['goods_listpic']['name']){
                LocalUpload::go_upload($_FILES['goods_listpic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $logicRes = $goodsLogic->editById($id, $params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新商品成功');
                jump('goods/index');
            } else {
                Yii::$app->getSession()->setFlash('error','更新商品失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }

    }

    public function actionDelete(){
        $id = Yii::$app->request->get('goods_id');
        $goodsLogic = new GoodsLogic();
        $logicRes = $goodsLogic->delete($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','删除成功');
        } else {
            Yii::$app->getSession()->setFlash('error','删除失败');
        }
        return $this->redirect(U('goods/index'));
    }

}